<%@ page language="java" contentType="text/html; charset=UTF-8" pageEncoding="UTF-8"%>
<%@ taglib uri="http://java.sun.com/jsp/jstl/core" prefix="c" %>   
<%@ taglib uri="http://java.sun.com/jsp/jstl/fmt" prefix="fmt"%>  
<%@ taglib uri="http://java.sun.com/jsp/jstl/functions" prefix="fn" %>  
<%@ taglib uri="http://shiro.apache.org/tags" prefix="shiro" %>       
<c:set var="ctx" value="${pageContext.request.contextPath}"/> 
<%@ taglib prefix="dict" uri="/WEB-INF/tld/dict.tld" %>  
<!DOCTYPE html PUBLIC "-//W3C//DTD HTML 4.01 Transitional//EN" "http://www.w3.org/TR/html4/loose.dtd">
<html>
<head>
<meta http-equiv="Content-Type" content="text/html; charset=UTF-8">
<jsp:include page="../include.jsp"/>
<script type="text/javascript">
	var dgFactory;	
	var dgCarrier;
	var itemsType;
	$(function() {
		 //初始化数组字典
		itemsType = <dict:getDictItem dictCode="DICT_CARRIER_TYPE"/>;
		initCombotree();
		loadOrg('${orgId}');
		setParentOrg();
		parent.$.messager.progress('close');
		$('#form').form({
			url : '${ctx}/org/addOrg',
			onSubmit : function() {
				validate = $(this).form('validate');
				if(validate && checkData()){
					return true;
				}else{
					return false;
				}				
			},
			success : function(result) {
				parent.$.messager.progress('close');
				result = $.parseJSON(result);
				if(result.success) {
					parent.$.modalDialog.orgTree.tree('reload');
					parent.$.modalDialog.handler.dialog('close');
					parent.$.messager.alert('系统提示', result.msg, 'info');
				} else {
					parent.$.messager.alert('错误', result.msg, 'error');
				}
			}
		});
	});
	
	//修改时，上级ID不能和当前ID相同
	function checkData(){
		var orgId = '${orgId}';
		if(orgId){
			var pId = $('#cc').combotree('getValue');
			if(orgId==pId){
				parent.$.messager.alert('错误', '上级节点不能和当前节点相同!', 'error');
				return false;
			}else{
				return true;
			}
		}else{
			return true
		}
	}
	
	function initCombotree(){
		$('#cc').combotree({    
			url: '${ctx}/org/getAllOrganization',    
		    required: true,
		    lines:true
		}); 
	}
	
	function setParentOrg(){
		var pId = "${pId}";
		if(pId !=""){
			$('#cc').combotree('setValue', pId);
		}
	}
	
	function loadOrg(id){
		if(id){
			$('#form').form('load','${ctx}/org/getOrgById?orgId='+id);
		}
	}
	
	function initFactory(){
		dgFactory = $('#dgFactory').datagrid({
			title:'烟厂信息',
			url:'${ctx}/smokeFactory/listSmokeFactory',
			nowrap:true,
			striped:true,
			idField:'smokeFactoryId',
			pagination:false,
			fit:true,
			fitColumns:true,			
			sortName:'SMOKE_FACTORY_CODE',
			sortOrder:'asc',
			rownumbers: true, //是否加行号 
			columns:[[	 
				 {field:'smokeFactoryId',checkbox :true},
				 {field:'smokeFactoryName',title:'烟厂名称',width:300,align:'center'},
				 {field:'smokeFactoryCode',title:'烟厂编码',width:300,align:'center'},
			]],
			toolbar : '#toolbarOrg',
			onDblClickRow: function(index, row){
				setFactoryValue(row);
				$('#wFactory').window('close');
			}
		});
	}
	
	function initCarrier(){
		dgCarrier = $('#dgCarrier').datagrid({
			title:'承运商信息配置',
			url:'${ctx}/carrier/listCarriers',
			nowrap:true,
			striped:true,
			idField:'carrierId',
			pagination:true,
			fit:true,
			fitColumns:true,
			sortName:'CARRIER_CODE',
			sortOrder:'asc',
			rownumbers: true, //是否加行号 
			columns:[[	 
				 {field:'carrierId',checkbox :true},
				 {field:'carrierCode',title:'承运商编码',width:120,align:'center'}, 
				 {field:'carrierName',title:'承运商名称',width:260,align:'left'},
				 {field:'carrierShortName',title:'承运商简称',width:80,align:'left'},
	    		 {field:'carrierType',title:'承运商类型',width:80,align:'center',formatter:formatterType},
			]],
			toolbar : '#toolbarCarr',
			onDblClickRow: function(index, row){
				setCarrierValue(row);
				$('#wCarrier').window('close');
			}
		})
	}
	
	function setFactoryValue(row){
		$('#deptName').textbox('setValue', row.smokeFactoryName);
		$('#deptCode').textbox('setValue', row.smokeFactoryCode);
		$('#deptAlias').textbox('setValue', row.smokeFactoryName);
	}
	
	function setCarrierValue(row){
		$('#deptName').textbox('setValue', row.carrierName);
		$('#deptCode').textbox('setValue', row.carrierCode);
		$('#deptAlias').textbox('setValue', row.carrierShortName);
	}
	
	function selectFactory(){
		$('#wFactory').window('open');
		initFactory();
		$('#dgFactory').datagrid("clearSelections");
	}
	
	function selectCarrier(){
		$('#wCarrier').window('open');
		initCarrier();
		$('#dgCarrier').datagrid("clearSelections");
	}
	
	function formatterType(value, row, index){
		var val =  $.getDictItemText(itemsType,value);
		return val;
	}
	
	function chooseOrg(){
		var rows = dgFactory.datagrid('getChecked');
		if (rows.length == 1) {
			setFactoryValue(rows[0]);
			$('#wFactory').window('close');
		}else{
			parent.$.messager.alert('系统提示', '请勾选一条记录！', 'warning');
		}
	}
	
	function chooseCarrier(){
		var rows = dgCarrier.datagrid('getChecked');
		if (rows.length == 1) {
			setCarrierValue(rows[0]);
			$('#wCarrier').window('close');
		}else{
			parent.$.messager.alert('系统提示', '请勾选一条记录！', 'warning');
		}
	}
</script>
</head>
<body>
	<div class="easyui-layout" data-options="fit:true,border:false">
		<div data-options="region:'center',border:false" title="" style="overflow: hidden;">			
			<form id="form" method="post">		
				<input name="orgId" type="hidden" value="${orgId}">	
				<div style="margin-bottom:5px;padding-top:30px;padding-left:50px">
	                <input labelAlign="right" class="easyui-textbox" value="" id="deptName" name="deptName" style="width:70%" data-options="label:'组织机构名称:',labelWidth:120,required:true">
	            </div>
	            <div style="margin-bottom:5px;padding-left:50px">
	                <input labelAlign="right" id="cc" name="parentId" value="" style="width:70%" data-options="label:'上级部门:',labelWidth:120,required:true">
	            </div>
	            <div style="margin-bottom:5px;padding-left:50px">
	                <input labelAlign="right" class="easyui-textbox" value="" id="deptCode" name="deptCode" style="width:70%" data-options="label:'机构编码:',labelWidth:120,required:true">
	            </div>
	            <div style="margin-bottom:5px;padding-left:50px">
	                <input labelAlign="right" class="easyui-textbox" value="" name="sortCode" style="width:70%" data-options="label:'排序:',labelWidth:120,required:true,validType:'number'">
	            </div>
	            <div style="margin-bottom:5px;padding-left:50px">
	                <input labelAlign="right" class="easyui-textbox" value="" id="deptAlias" name="deptAlias" style="width:70%" data-options="label:'行政组织简称:',labelWidth:120,required:true">
	            </div>
			 </form>		
		</div>
	</div>
</body>
</html>